package framework.ui;

import org.openems.entity.Entity;
import org.openems.entity.Key;
import org.openems.entity.PaneEntity;

import framework.DomainService;
import framework.EntityStoreListener;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Node;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;

public class EmsVerticalContainer {
	private VBox vbox;
	private MovablePane movablePane;
	
	public EmsVerticalContainer(Key key, final DomainService store, MovablePaneHandler handler) {
		vbox = new VBox();
		vbox.setStyle(" -fx-background-color: white;");
		
		final PaneEntity entity = (PaneEntity)store.getEntity(key);
		movablePane = new MovablePane(vbox, entity.getName(), handler);
		
		store.addEntityStoreListener(new EntityStoreListener() {
			@Override
			public void valueChanged(Entity entity) {
				PaneEntity e = (PaneEntity)entity;
				movablePane.layoutXProperty().set(e.getX());
				movablePane.layoutYProperty().set(e.getY());
			}			
		}, key);
		
		movablePane.layoutXProperty().set(entity.getX());
		movablePane.layoutYProperty().set(entity.getY());
		
		movablePane.layoutXProperty().addListener(new ChangeListener<Number>() {
			@Override
			public void changed(ObservableValue<? extends Number> arg0,
					Number arg1, Number arg2) {
				
				entity.setX(movablePane.getLayoutX());
				entity.setY(movablePane.getLayoutY());
				store.putEntity(entity);
			}
		});
		
		movablePane.layoutYProperty().addListener(new ChangeListener<Number>() {
			@Override
			public void changed(ObservableValue<? extends Number> arg0,
					Number arg1, Number arg2) {
				entity.setX(movablePane.getLayoutX());
				entity.setY(movablePane.getLayoutY());
				store.putEntity(entity);
			}
		});
	}

	public void add(Node node) {
		vbox.getChildren().add(node);
	}

	public Pane getPane() {
		return this.movablePane;
	}

	public void setOpacity(double d) {
		this.movablePane.setOpacity(d);
	}
}
